package com.example.thread.d8CompletableFuture1.supplyAsync1;

import com.example.thread.d8CompletableFuture1.SmallTool;

import java.util.concurrent.CompletableFuture;

// 等待上一个异步任务执行完毕之后，再去执行
// 连接两个有依赖关系的任务，结果由第二个任务返回
public class _04_thenComposeAndthenComposeAsync {
    public static void main(String[] args) {

        CompletableFuture<String> cf1 = CompletableFuture.supplyAsync(()-> {
            SmallTool.printTimeAndThread("厨师炒菜");
            SmallTool.sleepMillis(200);
            return "番茄炒蛋";
        }).thenComposeAsync(dish-> {      // thenCompose  and thenComposeAsync
            SmallTool.printTimeAndThread("服务员A 准备打饭，但是被领导叫走，打饭交接给服务员B");
            return CompletableFuture.supplyAsync(()-> {
                SmallTool.printTimeAndThread("服务员B 打饭");
                SmallTool.sleepMillis(100);
                return dish + " + 米饭";
            });
        });

        SmallTool.printTimeAndThread(String.format("%s,小白吃饭",cf1.join()));  // join（）等待任务执行结束，返回任务结束的结果
    }
}


